package cn.zn.framework.printer.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.zn.framework.printer.inter.PrintOperInter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;

import java.util.Map;

/**
 * 打印操作接口实现
 *
 * @author zn
 */
@Service
@Slf4j
public class PrintOperServiceImpl implements PrintOperInter {
    @Override
    public String getValueForTmpl(String prefix, String suffix, String tmplName, Map<String, Object> variables) {
        //thymeleaf构造模板引擎;给html文件赋值
        ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
        //默认是找classpath路径下的资源
        if (StrUtil.isNotEmpty(prefix)) {
            resolver.setPrefix(prefix);
        }
        //模板文件后缀
        if (StrUtil.isNotEmpty(suffix)) {
            resolver.setSuffix(suffix);
        }
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(resolver);
        //构造上下文
        Context context = new Context();
        if (variables != null && !variables.isEmpty()) {
            //设置变量
            context.setVariables(variables);
        }
        String value = templateEngine.process(tmplName, context);
        return value;
    }
}
